package com.jingyanku.struts2;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jingyanku.pojo.Board;
import com.jingyanku.pojo.Category;
import com.jingyanku.pojo.Thread;
import com.jingyanku.service.IBoardService;
import com.jingyanku.service.ICategoryService;
import com.jingyanku.service.IThreadService;
import com.opensymphony.xwork2.ActionSupport;

public class CategoryAction extends ActionSupport {
	private String result;
	private Category category;
	private ICategoryService<Category> categorySer;
	private IThreadService<Thread> threadSer;
	public List<Category> cateList = new ArrayList<Category>();
	public List<Thread> hotList = new ArrayList<Thread>();
	private boolean flag = false;

	public String list() {
		
		cateList = categorySer.list(
				" from Category as c where c.deleted = false ", 0,
				Integer.MAX_VALUE, null);
		hotList = threadSer.list(" from Thread as t where t.deleted = false order by t.hit desc  ", 0,
				2, null);
		 if(result!=null&&result.equals("admin"))
		 return "admin";
		 else
		return "list";
	}

	public String add() {
		category.setDateCreated(new Date());
		flag = categorySer.create(category);
		if (flag = false) {
			addActionMessage("类别 " + category.getName() + " 已经存在。");
		}
		this.setResult("add");
		return "success";
	}

	public String delete() {
		category = categorySer.find(Category.class, category.getId());
		category.setDeleted(true);
		categorySer.save(category);
		return "success";
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}


	public List<Category> getCateList() {
		return cateList;
	}

	public void setCateList(List<Category> cateList) {
		this.cateList = cateList;
	}

	public List<Thread> getHotList() {
		return hotList;
	}

	public void setHotList(List<Thread> hotList) {
		this.hotList = hotList;
	}
	public IThreadService<Thread> getThreadSer() {
		return threadSer;
	}

	public void setThreadSer(IThreadService<Thread> threadSer) {
		this.threadSer = threadSer;
	}

	public ICategoryService<Category> getCategorySer() {
		return categorySer;
	}

	public void setCategorySer(ICategoryService<Category> categorySer) {
		this.categorySer = categorySer;
	}


	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

}
